﻿/*该游戏的难点有三个：1，碰撞放置，我采用的是flash自带的hitTest类，然后计算撞击者的坐标，与被撞者周围六个点进行比较，
   然后放置距离最近的一个，如果最近的有其它小球，则放置次近的那个。2，搜索相同颜色的小球。先把发射球加入总数组，然后求出
   它周围的六个球，把其中颜色与发射球一致的单独存放，然后用递归的方式往周围搜索，其中有个关键点是要把他们中重复的祛除，递归的
   出口也是个难点，我用一个数组的长度当做标尺来实现的。3，悬空的小球的消除。也是用递归的方式，从顶部任意一个搜索与之连接的，
   然后看剩余的是否有小球出现在最顶部，如果有，继续搜索，直到顶部没有连接或着为零的情况出现。
   上述方法不一定是最佳之选，期待共同进步！
   以下几点声明：1，得分的效果实现用的是Gary AS University 的 PointBurst类，其它所有工作均属个人所做。
   2，声音来源于网络，并非原创。3，另外的附加类分别是sceneData(布置墙壁），GameSound(游戏声音），Bubble（生成随机颜色的
   小球），因比较简单，不再写注释。PointBurst类中有个绑定字体属性，因会使文件增大到6M以上，所以弃用，造成得分效果不好看。
   4，本人是as天地会的成员之一，即wonder。感谢天地会这个平台。
   此游戏或许还有一些不足，我会努力完善。* 欢迎交流：qq:93619312;
 个人space : http://space.actionscript3.cn/?uid/11148/      =================================================*/
//==========================================================
package {
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import fl.controls.Button;
	import fl.transitions.*;
	import fl.transitions.easing.Regular;

	public class Pop extends MovieClip {

		private static const W:uint = 445; //场地宽
		private static const H:uint = 510; //场地高
		private static const MAP_UNIT:uint = 30; //地图元素尺寸

		private static const L:uint = 15;
		private static const ROW:uint = 6; //初始球的row

		private static const TOTALROW:uint = 15; //总的row
		private static const TOTALCOL:uint = 13; //总的col
		private static const RADIUS:uint = 30; //球半径

		private static const MINI:uint = 3; //设置最小几次未得分增加小球
		private var _vx:Number;
		private var _vy:Number;
		private var _V:Number;
		private var initX:Number;
		private var initY:Number;
		private var initMouseX:Number;
		private var initMouseY:Number;
		//以上是子弹发射有关的参数
		private var bubbleArray:Array = new Array(); //用来存储所有小球的引用的二维数组，最常用的
		private var dropArr:Array = new Array(); //放置待消除的数组
		private var temDropArr:Array = new Array(); //临时放置待消除的数组
		private var bullet:Bubble; //发射者
		//静态布局参数
		private var tip:Tip = new Tip();
		private var txt:TextField;
		private var instr:TextField = new TextField();
		private var detail:TextField = new TextField();
		private var gameOverTxt:TextField = new TextField();
		private var scoreBoard:TextField;
		private var btn:Btn = new Btn(); //开始按钮
		private var button:Button = new Button(); //重玩按钮
		private var container:Sprite = new Sprite(); //初始界面容器
		private var textFormat:TextFormat = new TextFormat("宋体", 40, 0xff00ff);
		private var myFormat:TextFormat = new TextFormat("宋体", 14, 0xffffff);
		private var tForm:TextFormat = new TextFormat();
		private var wall:SceneData = new SceneData; //周围墙壁
		private var txtSpr:Sprite = new Sprite();
		//private var timer:Timer;
		private var row:uint; //bubbleArray的row
		private var col:uint; //bubbleArray 的 col
		private var core_r:uint; //搜索网络的起始球的row
		private var core_c:uint; //搜索网络的起始球的col

		private var lenArra:Array = new Array(); //衡量搜索相同颜色小球完毕的尺子，即递归出口
		private var dropLenArr:Array = new Array(); //衡量搜索相连接小球完毕的尺子
		private var sameBubbleTotal:Array; //搜索到相同小球最终放置的数组，如果大于二，drop
		private var totalScore:uint = 0; //总得分
		private var checkNum:uint = 0; //检查几次没得分
		private var finalScore:uint; //gameover时显示的总分
		private var finalScoreTxt:TextField = new TextField();

		public function Pop(){
			startGame();
		}

		private function startGame():void {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			//布置初始场景,可以以美工的方式实现，这里采取code
			for (var i:uint = 0; i < 50; i++){
				var ball = new Bubble();
				ball.x = Math.random() * W;
				ball.y = Math.random() * H;
				ball.alpha = Math.random() * 0.3;
				container.addChild(ball);

			}

			addChild(container);
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.mouseEnabled = false;
			txt.text = "泡泡龙";

			txt.setTextFormat(textFormat);
			container.addChild(txt);
			txt.x = 222.5 - txt.width / 2;
			txt.y = 100;

			container.addChild(btn);
			btn.x = 222.5 - btn.width / 2;
			btn.y = 330;
			//trace(txt.width)
			btn.addEventListener(MouseEvent.CLICK, run);

			container.addChild(tip);
			tip.x = 222.5 - btn.width / 2 + 130;
			tip.y = 80;


			instr.text = "操作说明";

			tForm.size = 16;
			tForm.color = 0xff33ff;
			instr.setTextFormat(tForm);
			container.addChild(txtSpr);
			txtSpr.addChild(instr);
			txtSpr.x = 222.5 - txtSpr.width / 2;
			txtSpr.y = 390;
			instr.mouseEnabled = false;
			txtSpr.addEventListener(MouseEvent.ROLL_OVER, instrFunc);

			checkNum = 0;
		}

		private function instrFunc(e:MouseEvent):void {
			//trace ("click")
			detail.autoSize = TextFieldAutoSize.LEFT;
			detail.text = "    点击鼠标左键发射,颜色相同的三个小球会消失,同时你会获得一定的分数,得分之后三次不得分小球会增加两行。";
			detail.setTextFormat(new TextFormat("宋体", 15, 0x3399ff));
			detail.x = 70;
			detail.y = 420;
			detail.width = 300;
			detail.wordWrap = true;
			detail.selectable = false;
			container.addChild(detail);


		}

		private function run(evt:MouseEvent):void {
			//布置游戏入局场景
			evt.stopPropagation();
			for (var i:uint; i < container.numChildren; i++){
				container.removeChild(container.getChildAt(0));
			}
			removeChild(container);
			//container = null;

			addChild(wall);
			scoreBoard = new TextField();
			scoreBoard.x = 50;
			scoreBoard.y = 470;
			addChild(scoreBoard);
			scoreBoard.text = "总得分:";
			//scoreBoard.border = true;
			scoreBoard.autoSize = TextFieldAutoSize.LEFT;
			//scoreBoard.background = true;
			//scoreBoard.backgroundColor = 0xff3344;
			scoreBoard.borderColor = 0xffffff;
			scoreBoard.setTextFormat(new TextFormat("宋体", 14, 0xffffff));
			//布置小球
			for (var r:uint = 0; r < TOTALROW; r++){
				bubbleArray[r] = new Array();
				if (r < ROW){

					for (var c:uint = 0; c < TOTALCOL + 2; c++){

						if (r % 2 == 0){
							if (c > 12){
								bubbleArray[r][c] == null;
							} else {

								bubbleArray[r][c] = new Bubble();

								addChild(bubbleArray[r][c]);
								bubbleArray[r][c].x = c * MAP_UNIT + MAP_UNIT;
								bubbleArray[r][c].y = r * MAP_UNIT + MAP_UNIT;
							}
						} else {
							//if (c!=2) {
							if (c >= 12){
								bubbleArray[r][c] == null;
							} else {

								bubbleArray[r][c] = new Bubble();
								addChild(bubbleArray[r][c]);
								bubbleArray[r][c].x = c * MAP_UNIT + 3 * MAP_UNIT / 2;
								bubbleArray[r][c].y = r * MAP_UNIT + MAP_UNIT;
							}
								//}
						}
					}
				}
			}
			bubbleArray.length = L;


			setBullet();
			stage.addEventListener(MouseEvent.CLICK, clickHandler);
		}

		//布子弹
		private function setBullet():void {

			bullet = new Bubble();
			addChild(bullet);
			bullet.x = MAP_UNIT * (L - 1) / 2;
			bullet.y = MAP_UNIT * (L);

		}

		private function clickHandler(evt:MouseEvent):void {

			stage.removeEventListener(MouseEvent.CLICK, clickHandler);
			initX = bullet.x;
			initY = bullet.y;
			shot(20);


		}

		//发射
		private function shot(v:Number):void {

			_V = v;
			initMouseX = mouseX;
			initMouseY = mouseY;

			_vx = _V * Math.cos(Math.atan((initMouseY - (initY + RADIUS / 2)) / (initMouseX - (initX + RADIUS / 2))));
			_vy = _V * Math.sin(Math.atan((initMouseY - (initY + RADIUS / 2)) / (initMouseX - (initX + RADIUS / 2))));

			addEventListener(Event.ENTER_FRAME, onFrameHandler);

		}

		private function onFrameHandler(e:Event):void {
			//子弹运动规则

			if (initMouseX - MAP_UNIT / 2 >= initX){
				bullet.x += _vx;
				bullet.y += _vy;
					//trace (initMouseX-MAP_UNIT/2,initX )
			} else if (initMouseX - MAP_UNIT / 2 < initX){
				bullet.x += -_vx;
				bullet.y += -_vy;

			}
			if (bullet.x < MAP_UNIT){
				bullet.x = MAP_UNIT;
				_vx *= -1;
			} else if (bullet.x > W - MAP_UNIT * 2){
				bullet.x = W - MAP_UNIT * 2;
				_vx *= -1;
			}
			if (bullet.y < MAP_UNIT){
				bullet.y = MAP_UNIT;
				_vy *= -1;
			}
			//如果超出场地重新布置子弹
			if (bullet.y > MAP_UNIT * (TOTALCOL + 3)){
				removeChild(bullet);
				bullet = null;

				setBullet();
				//加两行小球
				addBubbles();
				checkNum = 0;
				removeEventListener(Event.ENTER_FRAME, onFrameHandler);
				stage.addEventListener(MouseEvent.CLICK, clickHandler);

			}
			checkHit();

		}

		//检测碰撞，提取被撞者的row 和col
		private function checkHit():void {
			//trace (bubbleArray.length)
			for (var r:uint = 0; r < bubbleArray.length; r++){
				for (var c:uint = 0; c < TOTALCOL; c++){
					if (bubbleArray[r][c]){

						if (bullet.hitTestObject(bubbleArray[r][c])){
							GameSound.hitSound.play();
							_vx = 0;
							_vy = 0;
							row = r;
							col = c;

							setPosition();
							setBullet();
							break;
							return;
						}
					}
				}
			}
		}

		//第一个难点出现，即碰撞后的放置
		private function setPosition():void {
			stage.addEventListener(MouseEvent.CLICK, clickHandler);
			//在Bubble里面写了个getSuroundPosition()方法，找到周围的六个点
			var suroundPositionArr:Array = bubbleArray[row][col].getSuroundPosition();
			trace(suroundPositionArr);
			var distanceArr:Array = new Array();
			for (var i:uint = 0; i < suroundPositionArr.length; i++){
				//碰撞后的点与上述六个点的距离放入数组
				distanceArr.push([bullet.getDistance(suroundPositionArr[i]) / 10, i]);
			}
			//大小排序
			distanceArr.sort();
			// 先以最近的点放置
			setSuitablePlace(distanceArr[0][1]);
			function setSuitablePlace(num:uint):void {
				//很多特殊情况得一一手动排除，包括奇偶行的差别，还有墙壁
				switch (num){

					case 0:
						if (col == 0){

							if (row % 2 == 0){
								core_r = row - 1;
								core_c = col;
							} else {
								core_r = row - 1;
								core_c = col + 1;
							}
							bullet.x = suroundPositionArr[1][0];
							bullet.y = suroundPositionArr[1][1];
							break;


						} else {

							if (row % 2 == 0){

								core_r = row - 1;
								core_c = col - 1;
							} else {
								core_r = row - 1;
								core_c = col;
							}
							bullet.x = suroundPositionArr[0][0];
							bullet.y = suroundPositionArr[0][1];
							break;
						}
					case 1:
						if (col == 12){
							if (row % 2 == 0){

								core_r = row - 1;
								core_c = col - 1;
							} else {
								core_r = row - 1;
								core_c = col;
							}
							bullet.x = suroundPositionArr[0][0];
							bullet.y = suroundPositionArr[0][1];
							break;

						} else {

							if (row % 2 == 0){
								core_r = row - 1;
								core_c = col;
							} else {
								core_r = row - 1;
								core_c = col + 1;
							}
							bullet.x = suroundPositionArr[1][0];
							bullet.y = suroundPositionArr[1][1];
							break;
						}
					case 2:

						core_r = row;
						core_c = col + 1;
						bullet.x = suroundPositionArr[2][0];
						bullet.y = suroundPositionArr[2][1];


						break;
					case 3:
						//trace("3");
						if (col == 12){

							if (row % 2 == 0){
								core_r = row + 1;
								core_c = col - 1;
							} else {
								core_r = row + 1;
								core_c = col;
							}
							bullet.x = suroundPositionArr[4][0];
							bullet.y = suroundPositionArr[4][1];
							break;

						} else {


							if (row % 2 == 0){
								core_r = row + 1;
								core_c = col;
							} else {
								core_r = row + 1;
								core_c = col + 1;
							}
							bullet.x = suroundPositionArr[3][0];
							bullet.y = suroundPositionArr[3][1];
							break;
						}
					case 4:
						//trace ("4")
						if (col == 0){
							if (row % 2 == 0){
								core_r = row + 1;
								core_c = col;
							} else {
								core_r = row + 1;
								core_c = col;
							}
							bullet.x = suroundPositionArr[4][0];
							bullet.y = suroundPositionArr[4][1];
							break;

						} else {


							if (row % 2 == 0){
								core_r = row + 1;
								core_c = col - 1;
							} else {
								core_r = row + 1;
								core_c = col;
							}
							bullet.x = suroundPositionArr[4][0];
							bullet.y = suroundPositionArr[4][1];
							break;
						}
					case 5:
						//trace("5");
						core_r = row;
						core_c = col - 1;
						bullet.x = suroundPositionArr[5][0];
						bullet.y = suroundPositionArr[5][1];
						break;
					//default :
					//break;

				}
			}
			//如果最近的被占，找次近的
			if (bubbleArray[core_r][core_c] is Bubble){
				//trace("IS");
				setSuitablePlace(distanceArr[1][1]);

			}
			//最后把子弹放入总数组
			bubbleArray[core_r][core_c] = bullet;

			removeEventListener(Event.ENTER_FRAME, onFrameHandler);
			//开始以子弹为核心搜索
			startSearch();

			//结果大于2执行drop
			if (sameBubbleTotal.length > 2){
				checkNum = 0;
				addEventListener(Event.ENTER_FRAME, startEff);
				//下面是消除时的效果
				var ay:Number = 0.2;
				var vy:Number = 0;
				function startEff(e:Event):void {
					for (var k:uint = 0; k < sameBubbleTotal.length; k++){

						vy += ay;
						sameBubbleTotal[k].y += vy;
						sameBubbleTotal[k].alpha *= 0.7;

						if (sameBubbleTotal[0].alpha < 0.1){

							//trace("IS");
							for (var kk:uint = 0; kk < sameBubbleTotal.length; kk++){
								GameSound.disapearSound.play();
								var pointBurst:PointBurst = new PointBurst(sameBubbleTotal[kk].parent, "+1", sameBubbleTotal[kk].x, sameBubbleTotal[kk].y);

								//trace(sameBubbleTotal[kk].parent); 
								removeChild(sameBubbleTotal[kk]);

								bubbleArray[getRow(sameBubbleTotal[kk])][getCol(sameBubbleTotal[kk])] = null;
								sameBubbleTotal[kk] = null;

							}
							//trace(sameBubbleTotal[sameBubbleTotal.length-1]);
							//消除完检查是否该 gameover 
							if (sameBubbleTotal[sameBubbleTotal.length - 1] == undefined){
								trace("ye");
								for (var h:uint = 0; h < TOTALCOL - 1; h++){

									if (bubbleArray[13][h] is Bubble){
										gameOver();
										break;
									}
								}
							}
							//以消除一个球得一分计算这次的得分，并加入总分中
							var numPoints:uint = kk;
							addScore(numPoints);
							removeEventListener(Event.ENTER_FRAME, startEff);
							//难点二的第一次搜索，
							search1();
							break;
						}
					}
				}
					//trace(this.numChildren);

			} else {
				checkNum += 1;
				if (checkNum == MINI){ //看是否应该增加小球
					addBubbles();
				}
				//看是否gameover
				for (var h:uint = 0; h < TOTALCOL - 1; h++){

					if (bubbleArray[13][h] is Bubble){
						gameOver();
						break;
					}
				}
			}
		}

		//开始搜索
		private function startSearch():void {
			//先手动搜索子弹的周围
			var firstArr:Array = firstSearch(bubbleArray[core_r][core_c]);
			//trace("firstArr=",firstArr);

			if (!firstArr){
				sameBubbleTotal = null;

			} else {
				//如果有，继续搜索
				sameBubbleTotal = searchSameColor(firstArr);
					//trace(sameBubbleTotal);

			}

		}

		//搜索相同颜色的小球，即第一次递归
		private function searchSameColor(arr:Array):Array {

			for (var i:uint = 0; i < arr.length; i++){
				//结果再加入自己的数组
				arr = arr.concat(searchAroundSameColor(bubbleArray, getRow(arr[i]), getCol(arr[i])));
				//trace("get=",getRow(arr[i]));
				//祛除重复
				for (var g:int = 0, temp:Array = []; g < arr.length; g++){
					;
					if (temp.indexOf(arr[g]) == -1){
						temp.push(arr[g]);
					}
				}
				arr = temp;
			}
			//加入标尺
			lenArra.push(arr.length);
			//出口
			if (lenArra[lenArra.length - 1] == lenArra[lenArra.length - 2]){

				//trace(lenArra);
				return arr;
			} else {

				searchSameColor(arr);

				return arr;
			}
		}

		private function firstSearch(obj:Bubble):Array {
			var temArray:Array;
			var r:uint = getRow(obj);
			var c:uint = getCol(obj);
			//分几种情况
			if (r == 0){
				temArray = new Array(bubbleArray[r][c - 1], bubbleArray[r][c + 1], bubbleArray[r + 1][c - 1], bubbleArray[r + 1][c]);
					//trace("r==0");
			} else {

				if (r % 2 != 0){
					temArray = new Array(bubbleArray[r - 1][c], bubbleArray[r - 1][c + 1], bubbleArray[r][c - 1], bubbleArray[r][c + 1], bubbleArray[r + 1][c], bubbleArray[r + 1][c + 1]);
				} else {
					temArray = new Array(bubbleArray[r - 1][c - 1], bubbleArray[r - 1][c], bubbleArray[r][c - 1], bubbleArray[r][c + 1], bubbleArray[r + 1][c - 1], bubbleArray[r + 1][c]);
				}
			}
			//检查颜色类型是否一致，不一致要除掉
			for (var ii:int = temArray.length - 1; ii > -1; ii--){
				if (!(temArray[ii] is Bubble) || (temArray[ii] && temArray[ii].checkType() != bullet.checkType())){
					temArray.splice(ii, 1);
				}
			}
			return temArray;
		}

		//此递归函数的参数必须是个数组，此函数返回之
		private function searchAroundSameColor(arr:Array, r:uint, c:uint):Array {
			var temArr:Array;
			if (r % 2 != 0){

				temArr = new Array(arr[r - 1][c], arr[r - 1][c + 1], arr[r][c - 1], arr[r][c + 1], arr[r + 1][c], arr[r + 1][c + 1]);
					//}
			} else {
				if (r < 1){
					temArr = new Array(arr[r][c - 1], arr[r][c + 1], arr[r + 1][c - 1], arr[r + 1][c]);
				} else {
					temArr = new Array(arr[r - 1][c - 1], arr[r - 1][c], arr[r][c - 1], arr[r][c + 1], arr[r + 1][c - 1], arr[r + 1][c]);
				}
			}
			for (var ii:int = temArr.length - 1; ii > -1; ii--){
				if (!(temArr[ii] is Bubble) || (temArr[ii] && temArr[ii].checkType() != bullet.checkType())){
					temArr.splice(ii, 1);
				}
			}
			return temArr;
		}

		//第二个递归的基础，找连接的
		private function searchAroundNear(arr:Array, r:uint, c:uint):Array {
			var temArr:Array;
			if (r % 2 != 0){

				temArr = new Array(arr[r - 1][c], arr[r - 1][c + 1], arr[r][c - 1], arr[r][c + 1], arr[r + 1][c], arr[r + 1][c + 1]);

			} else {
				if (r < 1){
					temArr = new Array(arr[r][c - 1], arr[r][c + 1], arr[r + 1][c - 1], arr[r + 1][c]);
				} else {
					temArr = new Array(arr[r - 1][c - 1], arr[r - 1][c], arr[r][c - 1], arr[r][c + 1], arr[r + 1][c - 1], arr[r + 1][c]);
				}
			}
			for (var ii:int = temArr.length - 1; ii > -1; ii--){
				if (!(temArr[ii] is Bubble)){
					temArr.splice(ii, 1);
				}
			}
			return temArr;
		}

		//先从最顶层个一个开始
		private function search1():void {
			for (var c:uint = 0; c < TOTALCOL; c++){

				if (bubbleArray[0][c] is Bubble){

					var arrayT:Array = searchAround(bubbleArray[0][c]);
					//还是去掉重复的
					for (var g:int = 0, temp:Array = []; g < arrayT.length; g++){
						;
						if (temp.indexOf(arrayT[g]) == -1){
							temp.push(arrayT[g]);
						}
					}
					arrayT = temp;
					//trace("leng=",arrayT,arrayT.length);
					temDropArr = [];
					//把剩下的放入数组
					for (var i:uint = 0; i < TOTALROW; i++){
						for (var j:uint = 0; j < TOTALCOL; j++){
							if ((bubbleArray[i][j] is Bubble) && arrayT.indexOf(bubbleArray[i][j]) == -1){
								//trace("bubbleArray[i][j]=",bubbleArray[i][j]);
								temDropArr.push(bubbleArray[i][j]);
							}
						}
					}
					//trace("temDropArr=",temDropArr);
					search2();
					break;
				} else {
					continue;
				}
			}
		}

//第二次,执行递归。
		private function search2():void {


			for (var i:uint = 0; i < temDropArr.length; i++){

				if (temDropArr[i] && getRow(temDropArr[i]) == 0){

					var arrayT:Array = searchAround(bubbleArray[0][getCol(temDropArr[i])]);
					break;

				}
			}

			if (arrayT){
				//trace("arrayT.length =",arrayT.length);
			}
			//如果该数组的对象都悬空，drop
			if (i == temDropArr.length && getRow(temDropArr[i - 1]) != 0){
				//trace("yes");
				doDrop(temDropArr);
			}
			if (arrayT){
				dropArr = [];
				for (var j:int = 0; j < temDropArr.length; j++){

					if ((temDropArr[j] is Bubble) && arrayT.indexOf(temDropArr[j]) == -1){

						dropArr.push(temDropArr[j]);
					}
				}
				//trace("dropArr=",dropArr.length);
				if (dropArr[0]){

					temDropArr = dropArr;
				} else {
					return;
				}
				for (var k:int; k < temDropArr.length; k++){
					//判断递归出口
					if (temDropArr[k] is Bubble){
						//调用递归
						search2();
						break;
					} else {
						return;
					}
				}
			}
		}

//消除悬空的小球
		private function doDrop(array:Array):void {
			//trace("666666");
			addEventListener(Event.ENTER_FRAME, startEff);
			//添加消除时的效果
			var ay:Number = 0.3;
			var vy:Number = 0;
			function startEff(e:Event):void {
				for (var k:uint = 0; k < array.length; k++){
					GameSound.dropSound.play();
					vy += ay;
					array[k].y += vy;
					array[k].alpha *= 0.8;

					if (array[0].alpha < 0.1){
						//记分
						var numPoints:uint = array.length;
						addScore(numPoints);

						for (var kk:int = array.length - 1; kk > -1; kk--){
							//添加得分效果
							var pointBurst:PointBurst = new PointBurst(array[kk].parent, "+1", array[kk].x, array[kk].y);
							//执行消除
							removeChild(array[kk]);

							bubbleArray[getRow(array[kk])][getCol(array[kk])] = null;
							array[kk] = null;
							array.splice(kk, 1);
								//trace(pointBurst);
						}
						removeEventListener(Event.ENTER_FRAME, startEff);

						break;
					}
				}
			}


		}

//用递归的方法找与目标相连的
		private function searchAround(obj:Bubble):Array {
			var c:uint = getCol(obj);
			var arrayD:Array;
			var array1:Array;
			array1 = new Array(bubbleArray[0][c - 1], bubbleArray[0][c + 1], bubbleArray[1][c - 1], bubbleArray[1][c]);
			//祛除空对象
			for (var i:int = array1.length - 1; i > -1; i--){
				if (!(array1[i] is Bubble)){
					array1.splice(i, 1);
				}
			}
			//如果没有，要返回它自己到数组
			if (!(array1[0] is Bubble)){
				return new Array(obj);
			} else {
				//跟找相同颜色的方法相似，少了个检查颜色
				arrayD = search(array1);
				//这里便是递归
				function search(arr:Array):Array {

					for (var ii:uint = 0; ii < arr.length; ii++){

						arr = arr.concat(searchAroundNear(bubbleArray, getRow(arr[ii]), getCol(arr[ii])));
						for (var iii:int = arr.length - 1; iii > -1; iii--){
							if (!(arr[iii] is Bubble)){
								arr.splice(iii, 1);
							}
						}

						for (var g:int = 0, temp:Array = []; g < arr.length; g++){
							;
							if (temp.indexOf(arr[g]) == -1){
								temp.push(arr[g]);
							}
						}
						arr = temp;
					}
					dropLenArr.push(arr.length);

					if (dropLenArr[dropLenArr.length - 1] == dropLenArr[dropLenArr.length - 2]){

						return arr;
					} else {

						search(arr);

						return arr;
					}
				}
				return arrayD;
			}

		}

//加两行小球。
		private function addBubbles():void {
			GameSound.addSound.play();
			for (var r:uint = 0; r < bubbleArray.length; r++){
				for (var c:uint = 0; c < TOTALCOL; c++){
					if (bubbleArray[r][c] is Bubble){

						bubbleArray[r][c].y += 60;

					}
				}
			}
			var arr0:Array = new Array();
			var arr1:Array = new Array();
			for (var g:uint = 0; g < TOTALCOL; g++){
				var bubble0:Bubble = new Bubble();
				addChild(bubble0);
				bubble0.x = MAP_UNIT * g + MAP_UNIT;
				bubble0.y = MAP_UNIT;
				arr0.push(bubble0);
			}
			for (var h:uint = 0; h < TOTALCOL - 1; h++){
				var bubble1:Bubble = new Bubble();
				addChild(bubble1);
				bubble1.x = MAP_UNIT * h + 3 * MAP_UNIT / 2;
				bubble1.y = MAP_UNIT * 2;
				arr1.push(bubble1);
			}
			bubbleArray.unshift(arr1);
			bubbleArray.unshift(arr0);

//trace(bubbleArray[0]);
///trace(bubbleArray[2][1].checkType());
		}

//获得任意小球在bubbleArray中的row和col
		private function getRow(obj:Object):uint {
			if (obj){
				var r:uint;
				var c:uint;
				for (r = 0; r < TOTALROW; r++){
					for (c = 0; c < TOTALCOL; c++){

						if (bubbleArray[r][c] == obj){
							return r;
						}
					}
				}
			}
			for (var aa in bubbleArray) {
				for (var bb in bubbleArray) {
				trace("bubbleArray["+aa+"]["+bb+"]="+bubbleArray[aa][bb]);					
				}				
			}
			trace(r,"AAAAAAAAAAAAAAAAAAAA")
			return r;
		}

		private function getCol(obj:Object):uint {
			if (obj){
				var r:uint;
				var c:uint;
				for (r = 0; r < TOTALROW; r++){
					for (c = 0; c < TOTALCOL; c++){

						if (bubbleArray[r][c] == obj){
							return c;
						}
					}
				}
			}
			return c;
		}

//记分函数
		private function addScore(num:Number):void {

			totalScore += num;
//trace("kkkkkkkkkkkk",totalScore);
			scoreBoard.text = "总得分:" + String(totalScore);
			scoreBoard.setTextFormat(new TextFormat("宋体", 14, 0xffffff));
//trace("eeeee");
		}

//gameover函数，可以以美工的方式实现，这里采取code
		private function gameOver():void {
			GameSound.loseSound.play();
			scoreBoard.text = "";
			trace(totalScore);
			finalScore = totalScore;
			totalScore = 0;
			checkNum = 0;

			finalScoreTxt.x = W / 2 - finalScoreTxt.width / 2;
			finalScoreTxt.y = H / 2;
			finalScoreTxt.text = "您共得分：" + String(finalScore);
			finalScoreTxt.setTextFormat(new TextFormat("宋体", 50, 0xffffff));
			finalScoreTxt.autoSize = TextFieldAutoSize.CENTER;
			finalScoreTxt.background = true;
			finalScoreTxt.backgroundColor = 0x000000;
			finalScoreTxt.selectable = false;
			addChild(finalScoreTxt);

			button.x = W / 2 + 75;
			button.y = H - 30;
			addChild(button);
			button.label = "再玩";
			button.width = 60;
			button.setStyle("textFormat", myFormat);

			gameOverTxt.text = "游戏结束！！！";
			gameOverTxt.width = 95;
			gameOverTxt.height = 20;

			gameOverTxt.border = true;
			gameOverTxt.borderColor = 0xffffff;
//gameOverTxt.autoSize = TextFieldAutoSize.CENTER;
			gameOverTxt.setTextFormat(myFormat);
			gameOverTxt.mouseEnabled = false;
			gameOverTxt.x = W / 2 - 150;
			gameOverTxt.y = H - 30;
			addChild(gameOverTxt);
			stage.removeEventListener(MouseEvent.CLICK, clickHandler);
//trace("gameover");
			button.addEventListener(MouseEvent.CLICK, restart);
		}

//重新开始
		private function restart(e:MouseEvent):void {
//this.removeChild(this.numChildren);
//trace ("this =",this.numChildren)
			for (var r:uint = 0; r < L; r++){
				for (var c:uint = 0; c < TOTALCOL; c++){
					if (bubbleArray[r][c]){

						removeChild(bubbleArray[r][c]);
						bubbleArray[r][c] = null;
						delete bubbleArray[r][c];
					}
				}
			}
			removeChild(bullet);
			removeChild(wall);
			removeChild(button);
			removeChild(finalScoreTxt);
			removeChild(scoreBoard);
			removeChild(gameOverTxt);
			button.removeEventListener(MouseEvent.CLICK, restart);
			startGame();
		}
	}

}